package cn.hengzhu.main_manager.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hengzhu.main_manager.repository.dao.AccessRecordMapper;
import cn.hengzhu.main_manager.repository.dao.ManageCaseBoxMapper;
import cn.hengzhu.main_manager.repository.dao.MobileKeyMapper;
import cn.hengzhu.main_manager.repository.domain.AccessRecord;
import cn.hengzhu.main_manager.repository.domain.ManageCaseBox;
import cn.hengzhu.main_manager.repository.domain.MobileKey;
import cn.hengzhu.main_manager.service.MobileKeyService;
import cn.hengzhu.main_manager.utils.DateTimeHelper;

@Service
@Transactional(rollbackFor = Exception.class)
public class MobileKeyServiceImpl implements MobileKeyService {
	@Autowired
	private AccessRecordMapper accessRecordMapper;
	@Autowired
	private MobileKeyMapper mobileKeyMapper;
	@Autowired
	private ManageCaseBoxMapper manageCaseBoxMapper;

	@Override
	public void openBoxBySendAfter(ManageCaseBox manageCaseBox, String code, String phoneNumber, String caseNo,
			String expressNumber) throws Exception {
		// 插入存储数据
		Integer boxId = manageCaseBox.getId();
		Date date = new Date();
		AccessRecord accessRecord = new AccessRecord();
		accessRecord.setCreateTime(date);
		accessRecord.setCreateDay(DateTimeHelper.formatDateTimetoString(date, "yyyy-MM-dd"));
		accessRecord.setPhone(phoneNumber);
		accessRecord.setSource((byte) 1);
		accessRecord.setType((byte) 1);
		accessRecord.setUserId(expressNumber);
		accessRecord.setBoxId(boxId);
		accessRecordMapper.insertSelective(accessRecord);
		// 插入一条短信验证码的记录
		MobileKey mobileKeyRecord = new MobileKey();
		mobileKeyRecord.setCreateTime(date);
		mobileKeyRecord.setCode(code);
		mobileKeyRecord.setPhone(phoneNumber);
		mobileKeyRecord.setSource((byte) 0);
		mobileKeyRecord.setStatus((byte) 1);
		mobileKeyRecord.setBoxId(boxId);
		mobileKeyMapper.insertSelective(mobileKeyRecord);
		// 修改储物箱的状态
		ManageCaseBox manageBoxCaseRecord = new ManageCaseBox();
		manageBoxCaseRecord.setId(boxId);
		manageBoxCaseRecord.setUseStatus((byte) 1);
		manageBoxCaseRecord.setSaveTime(date);
		manageBoxCaseRecord.setUserId(expressNumber);
		manageBoxCaseRecord.setSaveSource((byte) 5);
		manageCaseBoxMapper.updateByPrimaryKeySelective(manageBoxCaseRecord);


	}

	/**
	 * @author liuCheng
	 * @since 2018年11月30日 下午4:48:57
	 * @param mobileKey
	 * @param manageCaseBox
	 *            取件逻辑
	 * @throws Exception
	 * 
	 */
	@Override
	public void openBoxByPickUp(MobileKey mobileKey, ManageCaseBox manageCaseBox) throws Exception {
		Date date = new Date();
		// 修改验证码状态为无效
		Integer id = mobileKey.getId();
		MobileKey mobileKeyRecord = new MobileKey();
		mobileKeyRecord.setId(id);
		mobileKeyRecord.setStatus((byte) 0);
		mobileKeyMapper.updateByPrimaryKeySelective(mobileKeyRecord);
		// 修改储物柜状态为空闲
		Integer boxId = manageCaseBox.getId();
		ManageCaseBox manageCaseBoxRecord = new ManageCaseBox();
		manageCaseBoxRecord.setId(boxId);
		manageCaseBoxRecord.setUseStatus((byte) 0);
		manageCaseBoxMapper.updateByPrimaryKeySelective(manageCaseBoxRecord);
		// 插入一条取物记录
		String phone = mobileKey.getPhone();
		AccessRecord accessRecordRecord = new AccessRecord();
		accessRecordRecord.setBoxId(boxId);
		accessRecordRecord.setCreateDay(DateTimeHelper.formatDateTimetoString(date, "yyyy-MM-dd"));
		accessRecordRecord.setCreateTime(date);
		accessRecordRecord.setPhone(phone);
		accessRecordRecord.setSource((byte) 6);
		accessRecordRecord.setType((byte) 2);
		accessRecordRecord.setUserId(phone);
		accessRecordMapper.insertSelective(accessRecordRecord);
	}

}
